package hu.bme.aut.consulttime.client;

import hu.bme.aut.consulttime.client.components.ModifyUser;
import hu.bme.aut.consulttime.client.components.StudentRootPanel;
import hu.bme.aut.consulttime.client.components.TutorRootPanel;
import hu.bme.aut.consulttime.entitydtos.UserDTO;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class ConsultTime implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	//private static final String SERVER_ERROR = "An error occurred while "
	//		+ "attempting to contact the server. Please check your network "
	//		+ "connection and try again.";
	
	private Label lblHelloFullName;
	
	private final UserServiceAsync userService = GWT
			.create(UserService.class);
	private UserDTO userData = null;
	private String role = null;
	
	private VerticalPanel verticalPanel = null;

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		
		RootPanel rootPanel = RootPanel.get();
		rootPanel.setSize("960px", "100%");
		
		verticalPanel = new VerticalPanel();
		rootPanel.add(verticalPanel);
		verticalPanel.setSize("100%", "100%");
		verticalPanel.setBorderWidth(10);
		verticalPanel.setSpacing(5);
		
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		verticalPanel.add(horizontalPanel);
		horizontalPanel.setSize("100%", "20%");
		horizontalPanel.setSpacing(5);
		
		HorizontalPanel horizontalPanel_1 = new HorizontalPanel();
		horizontalPanel.add(horizontalPanel_1);
		horizontalPanel_1.setSpacing(5);
		
		lblHelloFullName = new Label("");
		lblHelloFullName.setDirectionEstimator(false);
		horizontalPanel_1.add(lblHelloFullName);
		horizontalPanel_1.setCellVerticalAlignment(lblHelloFullName, HasVerticalAlignment.ALIGN_MIDDLE);
		lblHelloFullName.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		horizontalPanel.setCellHorizontalAlignment(lblHelloFullName, HasHorizontalAlignment.ALIGN_CENTER);
		lblHelloFullName.setWidth("148px");
		
		Button btnEditProfile = new Button("Edit Profile");
		btnEditProfile.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				/*userService.retrieveUserData( new AsyncCallback<UserDTO>() {
					@Override
					public void onFailure(Throwable caught) {
						// Show the RPC error message to the user
						Window.alert("Remote Procedure Call - Failure");
					}
					@Override
					public void onSuccess(UserDTO result) {
						new ModifyUser(result).center();
					}
				});	*/
				new ModifyUser(userData).center();
			}
		});
		horizontalPanel_1.add(btnEditProfile);
		horizontalPanel_1.setCellVerticalAlignment(btnEditProfile, HasVerticalAlignment.ALIGN_MIDDLE);
		horizontalPanel.setCellHorizontalAlignment(btnEditProfile, HasHorizontalAlignment.ALIGN_CENTER);
		
		Button btnLogout = new Button("Logout");
		btnLogout.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, "/j_spring_security_logout");
				try {
				    rb.sendRequest(null, new RequestCallback() {
				        public void onResponseReceived(Request request, Response response) {
				            //GWT.log("Logged user out: " + response.getStatusText());
				        	Window.Location.reload();
				        }
				        public void onError(Request request, Throwable caught) {
				            // try to recover somehow
				        }
				    });
				} catch (RequestException re) {
				}
			}
		});
		horizontalPanel.add(btnLogout);
		horizontalPanel.setCellHorizontalAlignment(btnLogout, HasHorizontalAlignment.ALIGN_RIGHT);
		
		userService.retrieveUserData( new AsyncCallback<UserDTO>() {
			@Override
			public void onFailure(Throwable caught) {
				// Show the RPC error message to the user
				Window.alert("Remote Procedure Call - Failure");
			}
			@Override
			public void onSuccess(UserDTO result) {
				lblHelloFullName.setText(result.getFirstName() + " " + result.getLastName() + " (" + result.getUserName() + ")");
				userData = result;
				
				userService.retrieveUserRole( new AsyncCallback<String>() {
					@Override
					public void onFailure(Throwable caught) {
						// Show the RPC error message to the user
						Window.alert("Remote Procedure Call - Failure");
					}
					@Override
					public void onSuccess(String result) {
						role = result;
						
						if (role.equals("ROLE_STUDENT")) {
							StudentRootPanel studentRootPanel = new StudentRootPanel(userData);
							verticalPanel.add(studentRootPanel);
							verticalPanel.setCellHeight(studentRootPanel, "100%");
							verticalPanel.setCellWidth(studentRootPanel, "100%");
							verticalPanel.setCellHorizontalAlignment(studentRootPanel, HasHorizontalAlignment.ALIGN_CENTER);
							verticalPanel.setCellVerticalAlignment(studentRootPanel, HasVerticalAlignment.ALIGN_MIDDLE);
							studentRootPanel.setSize("100%", "100%");
						}
						
						else if (role.equals("ROLE_TUTOR")) {
							TutorRootPanel tutorRootPanel = new TutorRootPanel(userData);
							verticalPanel.add(tutorRootPanel);
							verticalPanel.setCellHeight(tutorRootPanel, "100%");
							verticalPanel.setCellWidth(tutorRootPanel, "100%");
							verticalPanel.setCellHorizontalAlignment(tutorRootPanel, HasHorizontalAlignment.ALIGN_CENTER);
							verticalPanel.setCellVerticalAlignment(tutorRootPanel, HasVerticalAlignment.ALIGN_MIDDLE);
							tutorRootPanel.setSize("100%", "100%");
						}
					}
				});
				
			}
		});
		
		
		
//		final HorizontalSplitPanel horizontalSplitPanel = new HorizontalSplitPanel();
//		verticalPanel.add(horizontalSplitPanel);
//		horizontalSplitPanel.setSplitPosition("20%");
//		horizontalSplitPanel.setSize("800px", "500px");
//		
//		DecoratedStackPanel decoratedStackPanel = new DecoratedStackPanel();
//		horizontalSplitPanel.setLeftWidget(decoratedStackPanel);
//		decoratedStackPanel.setSize("120px", "100%");
		

		
		//Startup 
		//LoginPage login = new LoginPage();
		//horizontalSplitPanel.setRightWidget(login);
		//login.setSize("100%", "100%");
		
		// --------------------------- Login Stack ---------------------------
//		VerticalPanel Login = new VerticalPanel();
//		decoratedStackPanel.add(Login, "Login", false);
//		Login.setSize("100%", "100%");
//		
//		Button btnLogin = new Button("Login");
//		btnLogin.addClickHandler(new ClickHandler() {
//			public void onClick(ClickEvent event) {
//				LoginPage login = new LoginPage();
//				horizontalSplitPanel.setRightWidget(login);
//				login.setSize("100%", "100%");
//			}
//		});
//		Login.add(btnLogin);

//		// --------------------------- Tutor Stack ---------------------------
//		VerticalPanel Tutor = new VerticalPanel();
//		decoratedStackPanel.add(Tutor, "Tutor", false);
//		Tutor.setSize("100%", "100%");
//		
//		Button btnManageStudents = new Button("Manage Students");
//		btnManageStudents.addClickHandler(new ClickHandler() {
//			public void onClick(ClickEvent event) {
//				StudentPage sp = new StudentPage();
//				horizontalSplitPanel.setRightWidget(sp);
//				sp.setSize("100%", "100%");
//			}
//		});
//		
//		Tutor.add(btnManageStudents);
//		
//		Button btnManageProjects = new Button("Manage Projects");
//		btnManageProjects.addClickHandler(new ClickHandler() {
//			public void onClick(ClickEvent event) {
//				ProjectPage pp = new ProjectPage();
//				horizontalSplitPanel.setRightWidget(pp);
//				pp.setSize("100%", "100%");
//			}
//		});
//		Tutor.add(btnManageProjects);
//		
//		Button btnManageGroups = new Button("Manage Teams");
//		btnManageGroups.addClickHandler(new ClickHandler() {
//			public void onClick(ClickEvent event) {
//				TeamPage gp = new TeamPage();
//				horizontalSplitPanel.setRightWidget(gp);
//				gp.setSize("100%", "100%");
//			}
//		});
//		Tutor.add(btnManageGroups);
//		
//		// --------------------------- Student Stack ---------------------------
//		VerticalPanel Student = new VerticalPanel();
//		decoratedStackPanel.add(Student, "Student", false);
//		Student.setSize("100%", "100%");
		

		
		
	}
}
